<?php
include('lib/adodb-time.inc.php');
require_once "HTML/Template/IT.php";

function formatDate ($dbDate,$type)                                                     
{                                                                                       
        if ($dbDate == "0000-00-00" || $dbDate == "") {                                 
                return "";                                                              
        }                                                                               
        list($year,$month,$day) = sscanf($dbDate,"%d-%d-%d");                           
        if ($type == "long") {                                                          
                $dateF = adodb_date("l, F j, Y", adodb_mktime(0,0,0,$month,$day,$year));
        } else {                                                                        
                $dateF = sprintf("%02d/%02d/%04d",$month,$day,$year);                                           
        }                                                                               
        return $dateF;                                                                  
}                                                                                       
                                                                                        
function formatName ($first, $middle, $last, $suffix, $nick, $items)                    
{                                                                                       
        $formatName = "";                                                               
	$last = strtoupper($last);
        if (strpos($items, "l") !== FALSE && $last != "") {                             
                $formatName .= "<b>$last</b>,<br>&nbsp;&nbsp;";  }                                              
        if (strpos($items, "f") !== FALSE && $first != "") {                            
                $formatName .= "$first ";  }                                            
        if (strpos($items, "n") !== FALSE && $nick != "") {                             
                $formatName .= "($nick) ";  }                                           
        if (strpos($items, "m") !== FALSE && $middle != "") {                           
                $formatName .= "$middle ";  }                                           
        if (strpos($items, "s") !== FALSE && $suffix != "") {                           
                $formatName .= ", $suffix ";  }                                         
        return $formatName;                                                             
                                                                                        
}                                                                                       

function altColor($old)
{
	if ($old == "#DDDDDD") {
		return "#FFFFFF";
	} else {
		return "#DDDDDD";
	}
}



// BEGIN MAIN

if (isset($_GET['Action'])) {
	$action = $_GET['Action'];
} else {
	$action = "Show All";
}

if ($action == "Show All") {
	$doSearch = FALSE;
	$needle = "";
} else {
	if ( isset($_GET['needle']) ) {
		$doSearch = TRUE;
		$needle = $_GET['needle'];
	} else {
		$doSearch = FALSE;
		$needle = "";
	}
}

if ( isset($_GET['sort']) ) {
	$sort = $_GET['sort'];
} else {
	$sort = "name_asc";
}

$sort_name = "name_asc";
$sort_dob  = "dob_asc";
$sort_dod  = "dod_asc";
switch ($sort) {
case "dob_asc":
	$ordSQL = "`dob` ASC";
	$sort_dob = "dob_desc";
	break;
case "dob_desc":
	$ordSQL = "`dob` DESC";
	break;
case "dod_asc":
	$ordSQL = "`dod` ASC";
	$sort_dod = "dod_desc";
	break;
case "dod_desc":
	$ordSQL = "`dod` DESC";
	break;
case "name_desc":
	$sort_dir = "DESC";
	$ordSQL = "`last` $sort_dir, `first` $sort_dir, `middle` $sort_dir, `suffix` $sort_dir";
	break;
default:
	$sort = "name_asc";
	$sort_dir = "ASC";
	$ordSQL = "`last` $sort_dir, `first` $sort_dir, `middle` $sort_dir, `suffix` $sort_dir";
	$sort_name = "name_desc";
	break;
}

// SET UP TEMPLATE

$tpl = new HTML_Template_IT("templates");
$template = "index_tmpl.html";
$status = $tpl->loadTemplatefile($template, true, true);

$tpl->setVariable("SortName", $sort_name);
$tpl->setVariable("SortDOB",  $sort_dob);
$tpl->setVariable("SortDOD",  $sort_dod);
$tpl->setVariable("oldNeedle",$needle);

/* Connecting, selecting database */                         
$link = mysql_connect("localhost", "ftreeweb", "ftree_web")  
   or die("Could not connect : " . mysql_error());           
mysql_select_db("ftree") or die("Could not select database");

if ($doSearch) {
	$term = mysql_real_escape_string($needle); //protect from SQL injection
	$query  = "SELECT * FROM person WHERE `first` LIKE '%$term%' OR `last` LIKE '%$term%' OR `middle` LIKE '%$term%' OR `nick` LIKE '%$term%' ORDER BY " . $ordSQL;
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
} else {
	$query  = "SELECT * FROM person WHERE 1 ORDER BY " . $ordSQL;
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
}

$color = "#FFFFFF";
while ($data   = mysql_fetch_array($result, MYSQL_ASSOC)) {
	$tpl->setCurrentBlock("Indiv") ;
	$full = formatName($data['first'], $data['middle'], $data['last'], $data['suffix'], $data['nick'], "fmls");
	$dob = formatDate($data['dob'],"short");
	$dod = formatDate($data['dod'],"short");
	$tpl->setVariable("PersonID", $data['id']) ;
	$tpl->setVariable("FullName", $full) ;
	$tpl->setVariable("DOB", $dob) ;
	$tpl->setVariable("DOD", $dod) ;
	$tpl->setVariable("DOD", $dod) ;
	$color = altColor($color);
	$tpl->setVariable("RowBgCol", $color) ;
	$tpl->parseCurrentBlock("Indiv") ;
}

mysql_close($link);
                   
$tpl->show();      

?>
